<template>
  <div class="CloudConfig">
    <el-row :gutter="10">
      <el-col :span="10">
        <h3>AccessKey:</h3>
        <el-input v-model="AccessKey" :disabled="AkStatus"
                  style="margin-top: 10px;margin-bottom: 20px;width:70%"></el-input>
        <h3>AccessKey Secret:</h3>
        <el-input v-model="AccessKeySecret" show-password :disabled="AkStatus"
                  style="margin-top: 10px;margin-bottom: 20px;width:70%"></el-input>
        <el-row>
          <el-button style="margin-top: 10px" @click="SaveConfig" :disabled="AkStatus">保存配置</el-button>
          <el-button v-if="AkStatus === true" style="margin-top: 10px" @click="InitConfig">修改配置</el-button>
        </el-row>
      </el-col>
      <el-col :span="14">
        <el-table
            :data="tableData"
        >
          <el-table-column
              prop="ID"
              label="模板ID"
          >
          </el-table-column>
          <el-table-column
              prop="Title"
              label="模板名称"
          >
          </el-table-column>
          <el-table-column
              prop="date"
              label="创建时间"
          >
          </el-table-column>
          <el-table-column
              label="操作"
          >
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="danger"
                  @click="DeleteTemplate(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button style="margin-top: 20px;float:right" type="primary" @click="dialogVisible = true">添加启动模板</el-button>
      </el-col>
    </el-row>
    <el-dialog
        title="创建VPS配置"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="Close">
      <div class="" v-if="index === 0">
        <el-form :inline="true" :model="One" style="margin-top: 20px">
          <el-form-item label="备注名：" style="width:100%">
            <el-input style="width:300px" v-model="One.Title" placeholder="模板名称"></el-input>
          </el-form-item>
          <el-form-item label="模板ID：" style="width:100%">
            <el-input style="width:300px" v-model="One.ID" placeholder="模板ID"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="next">确定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "CloudConfig",
  data() {
    return {
      AccessKey: '',
      AccessKeySecret: '',
      AkStatus: false,
      tableData: [],
      One: {
        ID: '',
        Title: ''
      },
      dialogVisible: false,
      index: 0,
      newConfig: '下一步'
    }
  },
  mounted() {
    function formatDate(date) {
      let d = new Date(date),
          month = '' + (d.getMonth() + 1),
          day = '' + d.getDate(),
          year = d.getFullYear();

      if (month.length < 2) month = '0' + month;
      if (day.length < 2) day = '0' + day;
      return [year, month, day].join('-');
    }

    // 查询AK是否存在
    this.$axios2.post('/searchCloudConf').then((res) => {
      let data = res.data
      if (data.code === 200) {
        this.AccessKey = '666666'
        this.AccessKeySecret = '88888888'
        this.AkStatus = true
      } else {
        this.AkStatus = false
      }
    })
    // 查询所有模板
    this.$axios2.post('/allTemplate').then((res) => {
      let data = res.data.allTemplate
      let num = data.length
      for (let i = 0; i < num; i++) {
        let list = {}
        list.ID = data[i].templateId
        list.Title = data[i].title
        list.date = formatDate(data[i].CreatedAt)
        this.tableData.push(list)
      }
    })
  },
  methods: {
    SaveConfig() {
      if (this.AccessKey === '' || this.AccessKeySecret === '') {
        this.$message.error('请输入内容！')
      } else {
        this.$axios2.post('/saveCloudConf', {
          "accessKey": this.AccessKey,
          "accessKeySecret": this.AccessKeySecret
        }).then((res) => {
          let data = res.data
          if (data.code === 200) {
            this.$message.success("保存成功")
          } else {
            this.$message.error("服务器发生错误，请重试！")
          }
          this.$router.go(0)
        })
      }
    },
    InitConfig() {
      this.AccessKey = ''
      this.AccessKeySecret = ''
      this.AkStatus = false
    },
    Close() {
      this.dialogVisible = false
    },
    next() {
      let id = this.One.ID
      let title = this.One.Title
      this.$axios2.post('/newTemplate', {
        'templateId': id,
        'title': title
      }).then((res) => {
        let data = res.data
        if (data.code === 200) {
          this.$message.success('添加模板成功！')
          this.$router.go(0)
        } else if (data.code === 1008) {
          this.$message.error('服务器错误，请重试！')
        }
      })
    },
    DeleteTemplate(index, row) {
      this.$axios2.post('/deleteTemplate', {
        "templateId": row.ID,
        "title": row.Title
      }).then((res) => {
        let code = res.data.code
        if (code !== 200) {
          this.$message.error("删除失败，请重试！")
        } else {
          this.$router.go(0)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>